package com.websai.servlets;

import com.sai.clases.Grupo;
import com.sai.clases.Rubro;
import com.sai.transacciones.TRubro;
import com.sai.transacciones.Tgrupo;
import com.sai.utils.Utilities;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Rubros extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            int metodo = Integer.parseInt(request.getParameter("metodo"));
            switch(metodo){
                case 0:
                    Registrar(request, response);
                    break;
                case 1:
                    ConsultaGeneral(request, response);
                    break;
                case 2:
                    Consultar(request, response);
                    break;
                case 3:
                RegistrarGrupo(request, response);
                break;
            }
        } finally {            
            out.close();
        }
    }
    
    public void Registrar(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            String parcial = request.getParameter("Parcial");
            String descripcion = request.getParameter("Descripcion");
            String materia = request.getParameter("Materia");
            String profesor = request.getParameter("Profesor");
            
            if(Utilities.isReallyEmptyOrNull(parcial) && Utilities.isReallyEmptyOrNull(descripcion)
                    && Utilities.isReallyEmptyOrNull(materia) && Utilities.isReallyEmptyOrNull(profesor)){
                out.print("Blanco");
            } else if(Utilities.isReallyEmptyOrNull(parcial)) {
                out.print("Parcial");
            } else if(Utilities.isReallyEmptyOrNull(descripcion)) {
                out.print("Descripcion");
            } else if(Utilities.isReallyEmptyOrNull(materia)) {
                out.print("Materia");
            } else if(Utilities.isReallyEmptyOrNull(profesor)) {
                out.print("Profesor");
            } else {
                Rubro rubro = new Rubro();
                rubro.setParcial(parcial);
                rubro.setDescripcion(descripcion);
                rubro.setMateria(materia);
                rubro.setProfesor(profesor);
                
                String respuesta = TRubro.Registrar(rubro);
                if(respuesta.equals("OK")){
                    out.print("OK");
                } else {
                    out.print(respuesta);
                }
            } 
        } finally {
            out.close();
        }
    }
    
    public void RegistrarGrupo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
          
            String descripcion = request.getParameter("Descripcion");
            String profesor = request.getParameter("Profesor");
            String horario = request.getParameter("Horario");
            if( Utilities.isReallyEmptyOrNull(descripcion)
                  &&Utilities.isReallyEmptyOrNull(profesor)&&Utilities.isReallyEmptyOrNull(horario)){
                out.print("Blanco");
            } else if(Utilities.isReallyEmptyOrNull(descripcion)) {
                out.print("Decripcion");
            } else if(Utilities.isReallyEmptyOrNull(profesor)) {
                out.print("Profesor");
            } else if(Utilities.isReallyEmptyOrNull(horario)) {
                out.print("Horario");
             
            } else {
                Grupo grupo = new Grupo();
                grupo.setDescripcion(descripcion);
                grupo.setProfesor(profesor);
                grupo.setTurno(horario);
                
                
                String respuesta = Tgrupo.RegistrarGrupo(grupo, descripcion, profesor, horario);
                if(respuesta.equals("OK")){
                    out.print("OK");
                } else {
                    out.print(respuesta);
                }
            } 
        } finally {
            out.close();
        }
    }
    
    public void ConsultaGeneral(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
             out.println(TRubro.Consultar());
        } finally {
            out.close();
        }
    }
    
    public void Consultar(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
